package com.csun.dsms.service.impl;

import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.mapper.DsmsScheduleLogMapper;
import com.csun.dsms.model.domain.DsmsSchedule;
import com.csun.dsms.model.domain.DsmsScheduleLog;
import com.csun.dsms.model.dto.schedule.ScheduleLogQueryDto;
import com.csun.dsms.service.DsmsScheduleLogService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-23 15:34
 * @description: TODO
 */
@Service
@Transactional
public class DsmsScheduleLogServiceImpl extends BaseService<DsmsScheduleLog> implements DsmsScheduleLogService {

    @Resource
    private DsmsScheduleLogMapper dsmsScheduleLogMapper;

    @Override
    public Integer insert(DsmsSchedule dsmsSchedule, Date startTime, Date endTime) {

        DsmsScheduleLog dsmsScheduleLog = new DsmsScheduleLog();
        dsmsScheduleLog.setScheduleId(dsmsSchedule.getId());
        dsmsScheduleLog.setScheduleName(dsmsSchedule.getScheduleName());
        dsmsScheduleLog.setScheduleCode(dsmsSchedule.getScheduleCode());
        dsmsScheduleLog.setCron(dsmsSchedule.getCron());
        dsmsScheduleLog.setStartTime(startTime);
        dsmsScheduleLog.setEndTime(endTime);
        dsmsScheduleLog.setOperateTime(endTime.getTime() - startTime.getTime());

        return dsmsScheduleLogMapper.insertSelective(dsmsScheduleLog);
    }

    /**
     * 查到调度器日志记录 - 分页
     *
     * @param scheduleLogQueryDto   the schedule log query dto
     *
     * @return  the dsms schedule log list
     */
    @Override
    public List<DsmsScheduleLog> queryListWithPage(ScheduleLogQueryDto scheduleLogQueryDto) {

        PageHelper.startPage(scheduleLogQueryDto.getPageNum(), scheduleLogQueryDto.getPageSize());

        return  dsmsScheduleLogMapper.queryListWithPage(scheduleLogQueryDto);
    }
}
